package org.example.demo;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class DatabaseManagementApp extends Application {

    @Override
    public void start(Stage primaryStage) {
        // 创建主布局
        BorderPane root = new BorderPane();

        // 创建工具栏
        ToolBar toolBar = new ToolBar(
                new Button("文件"),
                new Button("编辑"),
                new Button("查看"),
                new Button("查询")
        );
        root.setTop(toolBar);

        // 创建导航树

        TreeView<String> navigationTree = new TreeView<>();
        TreeItem<String> database1Item = new TreeItem<>("数据库1");
        TreeItem<String> table1Item1 = new TreeItem<>("数据表1");
        TreeItem<String> table1Item2 = new TreeItem<>("数据表2");
        database1Item.getChildren().addAll(table1Item1, table1Item2);

        TreeItem<String> database2Item = new TreeItem<>("数据库2");
        TreeItem<String> table2Item1 = new TreeItem<>("数据表1");
        TreeItem<String> table2Item2 = new TreeItem<>("数据表2");
        database2Item.getChildren().addAll(table2Item1, table2Item2);

        navigationTree.setRoot(new TreeItem<>("数据库目录")); // 设置一个虚拟的根节点
        navigationTree.getRoot().getChildren().addAll(database1Item, database2Item);
        root.setLeft(navigationTree);

        // 创建主要内容区域（用于SQL查询和结果）
        TextArea queryArea = new TextArea();
        TextArea outputArea = new TextArea();
        VBox queryAndOutputBox = new VBox(10, queryArea, outputArea);
        root.setCenter(queryAndOutputBox);

        // 创建状态栏
        Label statusBar = new Label("就绪");
        root.setBottom(statusBar);

        // 设置组件的外边距
        BorderPane.setMargin(toolBar, new Insets(10));
        BorderPane.setMargin(navigationTree, new Insets(10));
        VBox.setMargin(queryAndOutputBox, new Insets(10));
        BorderPane.setMargin(statusBar, new Insets(10));

        // 创建场景
        Scene scene = new Scene(root, 800, 600);
        primaryStage.setTitle("数据库管理系统");
        primaryStage.setScene(scene);
        primaryStage.show();

//        // 添加上下文菜单1（右键点击数据库名称）
//        ContextMenu contextMenu1 = new ContextMenu();
//        MenuItem newTableItem = new MenuItem("新建数据表");
//        contextMenu1.getItems().add(newTableItem);
//        database1Item.addEventHandler(ContextMenuEvent.CONTEXT_MENU_REQUESTED, event -> {
//            contextMenu1.show(primaryStage, event.getScreenX(), event.getScreenY());
//        });

// 添加上下文菜单（右键点击数据表）
        ContextMenu contextMenu1 = new ContextMenu();
        MenuItem newTableItem1 = new MenuItem("新建数据表");
        MenuItem viewStructureItem = new MenuItem("查看表结构");
        MenuItem modifyStructureItem = new MenuItem("修改表结构");
        MenuItem deleteTableItem = new MenuItem("删除该数据表");
        MenuItem renameTableItem = new MenuItem("重命名");
        contextMenu1.getItems().addAll(newTableItem1, viewStructureItem, modifyStructureItem, deleteTableItem, renameTableItem);
        navigationTree.addEventHandler(ContextMenuEvent.CONTEXT_MENU_REQUESTED, event -> {
            System.out.println("右键点击 table1Item1");
            contextMenu1.show(primaryStage, event.getScreenX(), event.getScreenY());
        });

        // 点击空白处时隐藏菜单
        scene.addEventFilter(MouseEvent.MOUSE_CLICKED, event -> {
            contextMenu1.hide();
        });

        // 处理“新建数据表”选项的点击事件
        newTableItem1.setOnAction(event -> {
            // 打开一个新的窗口，让用户输入有关新数据表的信息
            Stage newTableStage = new Stage();
            newTableStage.setTitle("新建数据表");
            // ...（添加新数据表的界面元素，例如输入框、按钮等）
            newTableStage.show();
        });

    }

    public static void main(String[] args) {
        launch(args);
    }
}
